/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package entities;

import java.io.Serializable;
import java.util.Collection;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import vo.VOUser;

/**
 *
 * @author santiagobaldrich
 */
@Entity
@Table(name = "User")
@NamedQueries(
{
    @NamedQuery(name = "User.findAll", query = "SELECT u FROM User u"),
    @NamedQuery(name = "User.findByIdUser", query = "SELECT u FROM User u WHERE u.idUser = :idUser"),
    @NamedQuery(name = "User.findByHandle", query = "SELECT u FROM User u WHERE u.handle = :handle"),
    @NamedQuery(name = "User.findByFirstName", query = "SELECT u FROM User u WHERE u.firstName = :firstName"),
    @NamedQuery(name = "User.findByLastName", query = "SELECT u FROM User u WHERE u.lastName = :lastName"),
    @NamedQuery(name = "User.findByPassword", query = "SELECT u FROM User u WHERE u.password = :password"),
    @NamedQuery(name = "User.findByAddress", query = "SELECT u FROM User u WHERE u.address = :address"),
    @NamedQuery(name = "User.findByPhone", query = "SELECT u FROM User u WHERE u.phone = :phone"),
    @NamedQuery(name = "User.findByEmail", query = "SELECT u FROM User u WHERE u.email = :email")
})
public class User implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "idUser")
    private Integer idUser;
    @Basic(optional = false)
    @Column(name = "handle")
    private String handle;
    @Basic(optional = false)
    @Column(name = "firstName")
    private String firstName;
    @Basic(optional = false)
    @Column(name = "lastName")
    private String lastName;
    @Basic(optional = false)
    @Column(name = "password")
    private String password;
    @Column(name = "address")
    private String address;
    @Column(name = "phone")
    private String phone;
    @Basic(optional = false)
    @Column(name = "email")
    private String email;
    @JoinColumn(name = "idInstitution", referencedColumnName = "idInstitution")
    @ManyToOne(optional = false)
    private Institution institution;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
    private Collection<Submission> submissionCollection;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
    private Collection<UserStats> userStatsCollection;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
    private Collection<Contest> contestCollection;

    public User()
    {
    }

    public User(Integer idUser)
    {
        this.idUser = idUser;
    }

    public User(Integer idUser, String handle, String firstName, String lastName, String password, String email)
    {
        this.idUser = idUser;
        this.handle = handle;
        this.firstName = firstName;
        this.lastName = lastName;
        this.password = password;
        this.email = email;
    }

    public Integer getIdUser()
    {
        return idUser;
    }

    public void setIdUser(Integer idUser)
    {
        this.idUser = idUser;
    }

    public String getHandle()
    {
        return handle;
    }

    public void setHandle(String handle)
    {
        this.handle = handle;
    }

    public String getFirstName()
    {
        return firstName;
    }

    public void setFirstName(String firstName)
    {
        this.firstName = firstName;
    }

    public String getLastName()
    {
        return lastName;
    }

    public void setLastName(String lastName)
    {
        this.lastName = lastName;
    }

    public String getPassword()
    {
        return password;
    }

    public void setPassword(String password)
    {
        this.password = password;
    }

    public String getAddress()
    {
        return address;
    }

    public void setAddress(String address)
    {
        this.address = address;
    }

    public String getPhone()
    {
        return phone;
    }

    public void setPhone(String phone)
    {
        this.phone = phone;
    }

    public String getEmail()
    {
        return email;
    }

    public void setEmail(String email)
    {
        this.email = email;
    }

    public Institution getInstitution()
    {
        return institution;
    }

    public void setInstitution(Institution institution)
    {
        this.institution = institution;
    }

    public Collection<Submission> getSubmissionCollection()
    {
        return submissionCollection;
    }

    public void setSubmissionCollection(Collection<Submission> submissionCollection)
    {
        this.submissionCollection = submissionCollection;
    }

    public Collection<UserStats> getUserStatsCollection()
    {
        return userStatsCollection;
    }

    public void setUserStatsCollection(Collection<UserStats> userStatsCollection)
    {
        this.userStatsCollection = userStatsCollection;
    }

    public Collection<Contest> getContestCollection()
    {
        return contestCollection;
    }

    public void setContestCollection(Collection<Contest> contestCollection)
    {
        this.contestCollection = contestCollection;
    }

    @Override
    public int hashCode()
    {
        int hash = 0;
        hash += (idUser != null ? idUser.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object)
    {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof User))
        {
            return false;
        }
        User other = (User) object;
        if ((this.idUser == null && other.idUser != null) || (this.idUser != null && !this.idUser.equals(other.idUser)))
        {
            return false;
        }
        return true;
    }

    @Override
    public String toString()
    {
        return "entities.User[idUser=" + idUser + "]";
    }
    public VOUser getVO()
    {
        VOUser vo = new VOUser();
        vo.setAddress(address);
        vo.setEmail(email);
        vo.setFirstName(firstName);
        vo.setInstName(institution.getName());
        vo.setHandle(handle);
        vo.setIdUser(idUser);
        vo.setLastName(lastName);
        vo.setPassword(password);
        vo.setPhone(phone);
        return vo;
    }

    public void setVOData(VOUser vo)
    {
        setAddress(vo.getAddress());
        setEmail(vo.getEmail());
        setFirstName(vo.getFirstName());
        setLastName(vo.getLastName());
        setPassword(vo.getPassword());
        setPhone(vo.getPhone());
    }
}
